﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GBFinance.Entity;
using GBFinance.BLL;
using GBFinance.Web.Models;
using Webdiyer.WebControls.Mvc;
using HiLand.Utility4.MVC.Paging;
using HiLand.Utility.Paging;
using HiLand.General.Entity;
using HiLand.General.BLL;
using HiLand.Utility.Finance;
using HiLand.General;

namespace GBFinance.Web.Areas.Manage.Controllers
{
    /// <summary>
    /// 本类不向View公开，为SecuredLoan和UnSecuredLoan的基类，仅仅为子类提供公用方法。
    /// </summary>
    public class BaseLoanController : GBFinance.Web.Controllers.BaseLoanController
    {
        //
        // GET: /Manage/Loan/

        protected ActionResult LoanList(LoanTypes loanType, int id = 1, string viewName = "")
        {
            int pageIndex = id;
            int pageSize = Miscs.PageSizeManage;
            int startIndex = (pageIndex - 1) * pageSize + 1;
            string whereClause = string.Format(" LoanType= {0} ", (int)loanType);
            string orderClause = "LoanID DESC";

            PagedEntityCollection<LoanBasicEntity> entityList = LoanBasicBLL.GetPagedCollection(startIndex, pageSize, whereClause, orderClause);
            PagedList<LoanBasicEntity> pagedList = entityList.ToPagedList();
            if (viewName == "")
            {
                viewName = "LoanList";
            }
            return View(viewName, pagedList);
        }

        public ActionResult LoanDetails(Guid? loanGuid)
        {
            Guid loanGuidLocal = Guid.Empty;

            this.ViewData["loanGuid"] = loanGuidLocal;
            return View();
        }

        public ActionResult ScheduleList(Guid? loanGuid)
        {
            Guid loanGuidLocal = Guid.Empty;
            List<LoanScheduleEntity> scheduleList = new List<LoanScheduleEntity>();

            if (loanGuid.HasValue)
            {
                loanGuidLocal = loanGuid.Value;
                string whereClause = string.Format(" LoanGuid='{0}' ", loanGuidLocal.ToString());
                scheduleList = LoanScheduleBLL.GetList(whereClause);
            }

            this.ViewData["loanGuid"] = loanGuidLocal;
            return View(scheduleList);
        }

        //[HttpPost]
        //public ActionResult ScheduleList(Guid? loanGuid,bool? justPlaceHolder)
        //{


        //    Guid loanGuidLocal = Guid.Empty;
        //    List<LoanScheduleEntity> scheduleList = new List<LoanScheduleEntity>();

        //    if (loanGuid.HasValue)
        //    {
        //        loanGuidLocal = loanGuid.Value;
        //        LoanBasicEntity loanBasicInfo = LoanBasicBLL.Get(loanGuidLocal);
        //        if (loanBasicInfo != null)
        //        { 

        //        }


        //        string whereClause = string.Format(" LoanGuid='{0}' ", loanGuidLocal.ToString());
        //        scheduleList = LoanScheduleBLL.GetList(whereClause);
        //    }

        //    this.ViewData["loanGuid"] = loanGuidLocal;
        //    //this.ViewData["scheduleList"] = scheduleList;


        //    return View(scheduleList);
        //}


        public ActionResult LoanInfoForSchedule(Guid? loanGuid, bool isDisplayGenerateButton)
        {
            LoanBasicEntity loanBasicInfo = new LoanBasicEntity();
            if (loanGuid.HasValue)
            {
                loanBasicInfo = LoanBasicBLL.Get(loanGuid.Value);
            }

            this.ViewData["isDisplayGenerateButton"] = isDisplayGenerateButton;
            return View(loanBasicInfo);
        }

        [HttpPost]
        public ActionResult LoanInfoForSchedule(Guid? loanGuid, bool isDisplayGenerateButton, bool? justPlaceHolder)
        {
            LoanBasicEntity loanBasicInfo = new LoanBasicEntity();
            if (loanGuid.HasValue)
            {
                loanBasicInfo = LoanBasicBLL.Get(loanGuid.Value);
                if (loanBasicInfo != null)
                {
                    double monthRate = 0.04;
                    int loanTermCount = loanBasicInfo.LoanTermCount;
                    double totalAmount = (double)loanBasicInfo.LoanAmount;
                    PaymentTermType paymentTermType = PaymentTermType.Monthly;
                    DateTime firstPaymentDate = loanBasicInfo.LoanDate.AddMonths(1);
                    List<Payment> paymentList = CPMLoan.GetPaymentSchedule(monthRate, loanTermCount, totalAmount, paymentTermType, firstPaymentDate);
                    for (int i = 0; i < paymentList.Count; i++)
                    {
                        Payment payment = paymentList[i];
                        LoanScheduleEntity scheduleEntity = new LoanScheduleEntity();

                        scheduleEntity.LoanGuid = loanGuid.Value;
                        scheduleEntity.Interest = (decimal)payment.Interest;
                        scheduleEntity.ScheduleNo = i.ToString().PadLeft(3, '0');
                        scheduleEntity.ScheduleGuid = Guid.NewGuid();
                        scheduleEntity.Principal = (decimal)payment.Principal;
                        scheduleEntity.PaymentDate = payment.PaymentDate;

                        LoanScheduleBLL.Create(scheduleEntity);
                    }
                }
            }

            this.ViewData["isDisplayGenerateButton"] = isDisplayGenerateButton;
            return View(loanBasicInfo);
        }


        public ActionResult Payment(Guid scheduleGuid)
        {
            LoanScheduleEntity loanScheduleInfo = new LoanScheduleEntity();
            LoanBasicEntity loanBasicInfo = new LoanBasicEntity();


            loanScheduleInfo = LoanScheduleBLL.Get(scheduleGuid);


            if (loanScheduleInfo.LoanGuid != Guid.Empty)
            {
                loanBasicInfo = LoanBasicBLL.Get(loanScheduleInfo.LoanGuid);
            }


            this.ViewData["loanBasicInfo"] = loanBasicInfo;
            return View(loanScheduleInfo);
        }

        [HttpPost]
        public ActionResult Payment(Guid scheduleGuid, decimal? paidAmount)
        {
            Guid loanGuid = Guid.Empty;
            if (scheduleGuid != Guid.Empty && paidAmount.HasValue && paidAmount.Value > 0)
            {
                LoanScheduleEntity loanScheduleInfo = LoanScheduleBLL.Get(scheduleGuid);
                if (loanScheduleInfo != null)
                {
                    loanGuid = loanScheduleInfo.LoanGuid;
                    loanScheduleInfo.PaidDate = DateTime.Now;
                    //TODO:目前讲还款都放入PrincipalPaid内。
                    loanScheduleInfo.PrincipalPaid = paidAmount.Value;

                    LoanScheduleBLL.Update(loanScheduleInfo);
                }
            }

            return RedirectToAction("ScheduleList", new { loanGuid = loanGuid }); //View(loanScheduleInfo);
        }

    }
}
